package com.cadre.web.controller.officer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cadre.common.utils.R;
import com.cadre.common.utils.SecurityUtils;
import com.cadre.system.domain.officer.PostchangeEntity;
import com.cadre.system.service.NEWPostchangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"A-N职务变动--弹框"})
@RequestMapping({"/profess/postchange_n"})
@RestController
/* loaded from: NEWPostchangeController.class */
public class NEWPostchangeController {
    @Autowired
    private NEWPostchangeService postchangeService;

    @ApiImplicitParam(name = "params", value = "批次id,人员id,", required = true, paramType = "query", defaultValue = "{namelistid:\"\",personid:\"\"}")
    @GetMapping({"/list"})
    @ApiOperation("获取批次人员职务变动信息 --1=在职,2=拟任,3=拟免")
    public R list(@RequestParam Map<String, Object> params) {
        LambdaQueryWrapper<PostchangeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return R.ok().put("list", (Object) this.postchangeService.getBaseMapper().selectList(
                lambdaQueryWrapper.eq(PostchangeEntity::getNamelistid, params.get("namelistid")).
                        eq(PostchangeEntity::getPersonid, params.get("personid")).orderByAsc(PostchangeEntity::getSort)));
    }

    @GetMapping({"/info/{flowid}"})
    @ApiOperation("拟任--获取变动信息--编辑")
    public R info(@PathVariable("flowid") String flowid) {
        return R.ok().put("postchange", (Object) ((PostchangeEntity) this.postchangeService.getById(flowid)));
    }

    @PostMapping({"/save"})
    @ApiOperation("拟任--获取变动信息--新增")
    public R save(@RequestBody PostchangeEntity postchange) {
        postchange.setSubmissdept(SecurityUtils.getDeptId());
        this.postchangeService.save(postchange);
        return R.ok();
    }

    @PostMapping({"/update"})
    @ApiOperation("拟任or拟免or在职--修改--保存 (flag为数据库字段名=2拟任 flag=3拟免)")
    public R update(@RequestBody PostchangeEntity postchange) {
        this.postchangeService.updateById(postchange);
        return R.ok();
    }

    @PostMapping({"/delete"})
    @ApiOperation("拟任--获取变动信息--删除")
    public R delete(@RequestBody String[] flowids) {
        this.postchangeService.removeBatchByIds(Arrays.asList(flowids));
        return R.ok();
    }

    @PostMapping({"/getNew/{flag}"})
    @ApiImplicitParam(name = "params", value = "批次id,人员id,", required = true, paramType = "query", defaultValue = "{namelistid:\"\",personid:\"\"}")
    @ApiOperation("获取变动后的拟任and拟免：flag=1获取拟任，flag=2获取拟免，flag=3获取拟任拟免")
    public R getNewPost(@PathVariable("flag") Integer flag, @RequestBody Map<String, String> params) {
        Map<String, Object> map = new HashMap<>();
        if (flag != null) {
            String str = "";
            String str2 = "";
            String unitName = "";
            if (flag.intValue() == 1) {
                LambdaQueryWrapper<PostchangeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                for (PostchangeEntity postchangeEntity : this.postchangeService.getBaseMapper().selectList(
                        lambdaQueryWrapper.eq(PostchangeEntity::getNamelistid, params.get("namelistid")).
                                eq(PostchangeEntity::getPersonid, params.get("personid")).in(PostchangeEntity::getFlag, "1,2").
                                orderByAsc(PostchangeEntity::getA1915).orderByAsc(PostchangeEntity::getSort))) {
                    if (postchangeEntity.getA1920() != null) {
                        if (!unitName.equals(postchangeEntity.getA1916())) {
                            str = str + postchangeEntity.getA1916() + postchangeEntity.getA1920() + "，";
                        } else {
                            str = str.substring(0, str.length() - 1) + "、" + postchangeEntity.getA1920() + "，";
                        }
                    }
                    unitName = postchangeEntity.getA1916();
                }
            } else if (flag.intValue() == 2) {
                LambdaQueryWrapper<PostchangeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                for (PostchangeEntity postchangeEntity2 : this.postchangeService.getBaseMapper().selectList(
                        lambdaQueryWrapper.eq(PostchangeEntity::getNamelistid, params.get("namelistid")).
                                eq(PostchangeEntity::getPersonid, params.get("personid")).
                                eq(PostchangeEntity::getFlag, "3").orderByAsc(PostchangeEntity::getA1915).orderByAsc(PostchangeEntity::getSort))) {
                    if (!unitName.equals(postchangeEntity2.getA1916())) {
                        str2 = str2 + postchangeEntity2.getA1916() + postchangeEntity2.getA1920() + "，";
                    } else {
                        str2 = str2.substring(0, str2.length() - 1) + "、" + postchangeEntity2.getA1920() + "，";
                    }
                }
                LambdaQueryWrapper<PostchangeEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                if (this.postchangeService.getBaseMapper().selectList(lambdaQueryWrapper1.eq(PostchangeEntity::getNamelistid, params.get("namelistid")).
                        eq(PostchangeEntity::getPersonid, params.get("personid")).eq(PostchangeEntity::getFlag, "1")).size() == 0 && str2.length() > 0) {
                    str2 = "现职";
                }
            } else if (flag.intValue() == 3) {
                LambdaQueryWrapper<PostchangeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                for (PostchangeEntity postchangeEntity3 : this.postchangeService.getBaseMapper().selectList(
                        lambdaQueryWrapper.eq(PostchangeEntity::getNamelistid, params.get("namelistid")).
                                eq(PostchangeEntity::getPersonid, params.get("personid")).orderByAsc(PostchangeEntity::getSort))) {
                    if (postchangeEntity3.getFlag().shortValue() == 3) {
                        if (postchangeEntity3.getA1920() != null) {
                            str2 = str2 + postchangeEntity3.getA1916() + postchangeEntity3.getA1920() + "，";
                        }
                    } else if (!unitName.equals(postchangeEntity3.getA1916())) {
                        str = str + postchangeEntity3.getA1916() + postchangeEntity3.getA1920() + "，";
                    } else {
                        str = str.substring(0, str.length() - 1) + "、" + postchangeEntity3.getA1920();
                    }
                }
            }
            if (str.length() > 0) {
                map.put("nrzw", str.substring(0, str.length() - 1));
            }
            if (str2.length() > 0) {
                if (str2.equals("现职")) {
                    map.put("nmzw", str2);
                } else {
                    map.put("nmzw", str2.substring(0, str2.length() - 1));
                }
            }
        }
        return R.ok().put("map", (Object) map);
    }

    @PostMapping({"/updateOrder"})
    @ApiOperation("职务变动排序")
    public R updateOrder(@RequestBody List<PostchangeEntity> entityList) {
        if (entityList.size() > 0) {
            for (int i = 0; i < entityList.size(); i++) {
                this.postchangeService.updateByPrimaryKeySelective(entityList.get(i));
            }
        }
        return R.ok();
    }

    @PostMapping({"/searchUnitByKey"})
    @ApiOperation("单位搜索")
    public R searchUnitByKey() {
        return R.ok().put("list", (Object) this.postchangeService.searchUnitByKey());
    }
}
